package com.sx.pipeline.ui.ledger

import android.app.Application
import android.text.TextUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.bus.RxBus
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.event.DeviceEvent
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.EquipmentAddDTO
import com.sx.pipeline.repos.http.res.DeviceInfoVo
import com.sx.pipeline.repos.http.res.Ipage
import com.sx.pipeline.repos.http.res.ReleaseTaskVo

/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description: TODO
 */
class LedgerVM(app: Application) : CYBaseViewModel(app) {

    /**
     * 添加设备
     */
    fun equipmentAdd(
        name: String, //设备名称
        manufacturer: String,//制造厂家
        productModel: String,//产品型号
        productNumber: String,//产品编号
        productionDate: String,//出厂日期
        unitId: Int, //单位id
        contact: String, //联系人
        contactPhone: String,//联系电话
        productImage: String //设备图片
    ) {
        val dto = EquipmentAddDTO(
            0,
            name,
            manufacturer,
            productModel,
            productNumber,
            productionDate,
            unitId,
            contact,
            contactPhone,
            productImage
        )
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showShort("请填写设备名称")
            return
        }
        if (TextUtils.isEmpty(manufacturer)) {
            ToastUtils.showShort("请填写产品制造厂家")
            return
        }
        if (TextUtils.isEmpty(productModel)) {
            ToastUtils.showShort("请填写产品型号")
            return
        }
        if (TextUtils.isEmpty(productNumber)) {
            ToastUtils.showShort("请填写产品编号")
            return
        }
        if (TextUtils.isEmpty(productionDate)) {
            ToastUtils.showShort("请选择出厂日期")
            return
        }

        if (unitId == 0) {
            ToastUtils.showShort("请填写设备所属单位")
            return
        }
        if (TextUtils.isEmpty(contact)) {
            ToastUtils.showShort("请填写联系人")
            return
        }
        if (TextUtils.isEmpty(contactPhone)) {
            ToastUtils.showShort("请填写联系电话")
            return
        }
        if (TextUtils.isEmpty(contactPhone)) {
            ToastUtils.showShort("请填写联系电话")
            return
        }
        if (TextUtils.isEmpty(productImage)) {
            ToastUtils.showShort("请上传设备图片")
            return
        }
        addSubscribe(
            mModel.equipmentAdd(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {

                    override fun onResult(result: Any) {
                        ToastUtils.showShort("设备添加完成")
                        //通知列表刷新
                        RxBus.getDefault().post(DeviceEvent())
                        finish()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")

                    }
                })
        )
    }

    /**
     * 编辑设备
     */
    fun equipmentEdit(
        id: Int,
        name: String, //设备名称
        manufacturer: String,//制造厂家
        productModel: String,//产品型号
        productNumber: String,//产品编号
        productionDate: String,//出厂日期
        unitId: Int, //单位id
        contact: String, //联系人
        contactPhone: String,//联系电话
        productImage: String //设备图片
    ) {
        val dto = EquipmentAddDTO(
            id,
            name,
            manufacturer,
            productModel,
            productNumber,
            productionDate,
            unitId,
            contact,
            contactPhone,
            productImage
        )
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showShort("请填写设备名称")
            return
        }
        if (TextUtils.isEmpty(manufacturer)) {
            ToastUtils.showShort("请填写产品制造厂家")
            return
        }
        if (TextUtils.isEmpty(productModel)) {
            ToastUtils.showShort("请填写产品型号")
            return
        }
        if (TextUtils.isEmpty(productNumber)) {
            ToastUtils.showShort("请填写产品编号")
            return
        }
        if (TextUtils.isEmpty(productionDate)) {
            ToastUtils.showShort("请选择出厂日期")
            return
        }

        if (unitId == 0) {
            ToastUtils.showShort("请填写设备所属单位")
            return
        }
        if (TextUtils.isEmpty(contact)) {
            ToastUtils.showShort("请填写联系人")
            return
        }
        if (TextUtils.isEmpty(contactPhone)) {
            ToastUtils.showShort("请填写联系电话")
            return
        }
        if (TextUtils.isEmpty(contactPhone)) {
            ToastUtils.showShort("请填写联系电话")
            return
        }
        if (TextUtils.isEmpty(productImage)) {
            ToastUtils.showShort("请上传设备图片")
            return
        }
        addSubscribe(
            mModel.equipmentEdit(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {

                    override fun onResult(result: Any) {
                        ToastUtils.showShort("设备编辑完成")
                        //通知列表刷新
                        RxBus.getDefault().post(DeviceEvent())
                        finish()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")

                    }
                })
        )
    }

    /**
     * 获取台账设备列表
     */
    var page = 1
    val deviceListEvent: SingleLiveEvent<List<DeviceInfoVo>> = SingleLiveEvent()
    fun getEquipmentList(key: String) {
        addSubscribe(
            mModel.getEquipmentList(key, page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<DeviceInfoVo>, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Ipage<DeviceInfoVo>) {
                        deviceListEvent.postValue(result.records)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 获取台账设备详情
     */
    val deviceDetailDataEvent: SingleLiveEvent<DeviceInfoVo> = SingleLiveEvent()
    fun getDeviceDetail(id: Int) {
        addSubscribe(
            mModel.getDeviceDetail(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<DeviceInfoVo, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: DeviceInfoVo) {
                        deviceDetailDataEvent.postValue(result)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 删除台账设备
     */
    fun getEquipmentDelete(id: Int) {
        addSubscribe(
            mModel.getEquipmentDelete(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        ToastUtils.showShort("设备删除完成")
                        //通知列表刷新
                        RxBus.getDefault().post(DeviceEvent())
                        finish()
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }
}